package com.sise.rsc.service;

import com.sise.rsc.entity.Courier;
import com.sise.rsc.entity.CourierStatus;
import com.sise.rsc.entity.PackageAction;
import com.sise.rsc.entity.WorkingArea;
import com.sise.rsc.repository.CourierRepository;
import com.sise.rsc.repository.CourierStatusRepository;
import com.sise.rsc.repository.PackageActionRepository;
import com.sise.rsc.repository.WorkingAreaRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Console;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CourierService {
    @Resource
    private CourierRepository courierRepository;
    @Resource
    private CourierStatusRepository courierStatusRepository;

    @Resource
    private PackageActionRepository packageActionRepository;
    @Resource
    private WorkingAreaRepository workingAreaRepository;
    //查询所有快递员信息
    public List<Courier> findAllCourier(){
        return courierRepository.findAllCourier();
    }
    //通过用户ID查询快递员信息
    public Courier findCourierByUserId(int uid){
        return courierRepository.findCourierByUserId(uid);
    }

    //改变工作状态
    public void changStateToWorking(Courier courier){

        Date nowTimeOfDay=new Date();
        List<CourierStatus> csl=courierStatusRepository.findCourierStatusesByTimeAnAndCourierId(nowTimeOfDay,courier.getId());
        System.out.println("the"+csl.size());
        if (csl.size()==0){
        CourierStatus courierStatus = new CourierStatus();
        courierStatus.setTime(new Date());
        courierStatus.setCourier(courier);
        courierStatus.setStatus("正常");
        courierStatusRepository.save(courierStatus);}
        else {
            System.out.println("工号姓名为："+courier.getNo()+courier.getRealName()+"的快递员重复签到");
        }
    }

    //查找相关工作区域名字对应的所有快递员
    public List<Courier> findCouriersByWorkingAreaAddressNameLike(String address){
        return courierRepository.findCouriersByWorkingAreaAddressNameLike(address);
    }
    //通过签到状态和快递员ID查找所有签到状态
    public List<CourierStatus> findCourierStatusesByStatusAndCourierId(String state,int cId){
        return courierStatusRepository.findCourierStatusesByStatusAndCourierId(state,cId);
    }

    //通过快递员ID查找某一月收到快递的数量
    public int getThisMonthFetchNumByCourierId(int courierId){
        Calendar calendar=Calendar.getInstance();
        List<PackageAction> packageActions=packageActionRepository.findThisMonthFetchNumByCourierId(courierId,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1);
//        System.out.println("=========查询某一个月=========="+packageActions.get(0).toString());
//        System.out.println("=========查询某一个月fetch=========="+calendar.get(Calendar.YEAR)+"and"+calendar.get(Calendar.MONTH));

        return packageActions.size();
    }
    //通过快递员ID查找某一月收到快递的数量
    public int getThisMonthDeliverNumByCourierId(int courierId){
        Calendar calendar=Calendar.getInstance();
        List<PackageAction> packageActions=packageActionRepository.findThisMonthDeliverNumByCourierId(courierId,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1);
//        System.out.println("=========查询某一个月=========="+packageActions.get(0).toString());
//        System.out.println("=========查询某一个月deliver==========");
        return packageActions.size();
    }

    //通过快递员的用户ID查找对应工作区域
    public String findWorkingAreaByUserId(int userId){
        Courier courier=courierRepository.findCourierByUserId(userId);
        System.out.println(courier.getWorkingArea()+"测试有没有工作区名字");
        WorkingArea workingArea=workingAreaRepository.findWorkingAreaById(courier.getWorkingArea().getId());
        return workingArea.getAddressName();
    }
}
